package jia.core.graphics;

import jia.core.Compass;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Hashtable;
import jia.game.GenericFlags;
import org.apache.log4j.Logger;

//====================================================

/**
 * @author Alec Lanter
 */
public class FirstPersonWallImageParser extends AbstractWallImageParser {
    private static final Logger log = Logger.getLogger(FirstPersonWallImageParser.class);
    
    //====================================================
    /**
     * @see net.sf.jia.framework.graphics.WallImageParser#getWallsetImage(java.lang.String)
     */
    public WallImage getWallsetImage(String imageResource) {
        URL imgUrl = getClass().getClassLoader().getResource(imageResource);
        
        if (imgUrl == null)
            throw new RuntimeException("Image resource '" + imageResource + "' not found.");
        
        BufferedImage img;
        try {
            img = ImageIO.read(imgUrl);
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
            throw new RuntimeException("Exception loading image resource '" + imageResource + "': " + e.getMessage(), e);
        }
        
        FirstPersonWallImage myWall = new FirstPersonWallImage();
        myWall.setImgResourceName(imageResource);
        
        int[] transColor = img.getRaster().getPixel(0, 0, new int[3]);
        img = setTransparency(img, transColor);
        imgProps = null;
        String[] propNames = img.getPropertyNames();
        if (propNames != null) {
            imgProps = new Hashtable();
            for (String propName : propNames) {
                imgProps.put(propName, img.getProperty(propName));
            }
        }
        
        // Far distance
        myWall.setTexture(Compass.NORTH, 2, makeSubimage(img, 132, 2, 16, 19, 3));
        myWall.setTexture(Compass.EAST, 2, makeSubimage(img, 262, 2, 16, 58));
        myWall.setTexture(Compass.WEST, 2, makeSubimage(img, 392, 2, 16, 58));
        
        // Mid distance
        myWall.setTexture(Compass.NORTH, 1, makeSubimage(img, 522, 2, 48, 58));
        myWall.setTexture(Compass.EAST, 1, makeSubimage(img, 2, 146, 32, 136));
        myWall.setTexture(Compass.WEST, 1, makeSubimage(img, 132, 146, 32, 136));
        
        // Near distance
        myWall.setTexture(Compass.NORTH, 0, makeSubimage(img, 262, 146, 112, 134));
        myWall.setTexture(Compass.EAST, 0, makeSubimage(img, 392, 146, 32, 212));
        myWall.setTexture(Compass.WEST, 0, makeSubimage(img, 522, 146, 32, 212));
        
        return myWall;
    }
}
